//
// Created by Zhang on 2022/3/17.
//
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <string.h>
#include <stdlib.h>

//book q3
class Complex {
private:
    double real;
    double imaginary;
public:
    Complex(double r, double i) : real(r), imaginary(i) {

    };

    double r() {
        return real;
    }

    double i() {
        return imaginary;
    }

    void print();
//    Complex add(Complex a,Complex b);
};

void Complex::print() {
    std::cout << real << "+ i*" << imaginary << std::endl;

}

Complex add(Complex a, Complex b) {
    double r = a.r() + b.r();
    double i = a.i() + b.i();
    return Complex(r, i);
}

//book q4
class s {
public:

    std::string a;

    s(std::string b) : a(b) {};

    int len();

    int is_empty();

    void empty();

    void set(std::string);

    int compare(s n);

    int find(s n);
};

int s::len() {
    return a.length();
}

int s::is_empty() {
    return (a.length() == 0) ? 1 : 0;
}

void s::empty() {
    a = "";
}

void s::set(std::string b) {
    a = b;
}

int s::compare(s n) {
    return a.compare(n.a);
}

int s::find(s n) {
    return a.find(n.a);
}

//book q5
class Point {
private:
    double x;
    double y;
public:
    Point(double x, double y) : x(x), y(y) {};

    std::vector<double> value() {
        std::vector<double> point;
        point.push_back(x);
        point.push_back(y);
        return point;
    }

    double distance(Point p);
};

double Point::distance(Point p) {
    auto point = p.value();
    return sqrt((x - point[0]) * (x - point[0]) + (y - point[1]) * (y - point[1]));
}

//ppt 3
class CPoint {
private:
    double x;
    double y;
public:
    CPoint(double xx = 60, double yy = 75) : x(xx), y(yy) {};

    void display();

    void setpoint(double x, double y);
};

void CPoint::display() {
    std::cout << x << "," << y << std::endl;
}

void CPoint::setpoint(double xx, double yy) {
    x = xx;
    y = yy;
}

//ppt 4
class CTest {
private:
    int a, b;
public:
    void init(int aa, int bb);

    void point();

};

void CTest::init(int aa, int bb) {
    a = aa;
    b = bb;
}

void CTest::point() {
    std::cout << a << "-" << b << "=" << a - b << std::endl;
}

//ppt chapter 2 q1
class CSample {
    char *p1, *p2;
public:
    void init(char *s1, char *s2);

    void print() {
        std::cout << "p1=" << p1 << "\n" << "p2=" << p2 << "\n";
    }

    void copy(CSample &one);

    void free();
};

void CSample::init(char *s1, char *s2) {
    p1 = (char *) new char[strlen(s1) + 1];
    p2 = (char *) new char[strlen(s2) + 1];
    strcpy(p1, s1);
    strcpy(p2, s2);
}

void CSample::copy(CSample &one) {
    delete[]p1;
    delete[]p2;
    p1 = (char *) new char[strlen(one.p1) + 1];
    p2 = (char *) new char[strlen(one.p2) + 1];
    strcpy(p1, one.p1);
    strcpy(p2, one.p2);

}

void CSample::free() {
    delete[]p1;
    delete[]p2;
}

//ppt chapter 2 q2
class CArray {
    int nSizeOfInt;
    int nNUmOfInt;
    int nSizeOfFloat;
    int nNumOfFloat;
    int *pInt;
    float *pFloat;
public:
    CArray(int nIntSize = 100, int nFloatSize = 200);

    void put(int n);

    void put(float x);

    int getInt(int index);

    float getFloat(int index);

    ~CArray();

    void print();

};

CArray::CArray(int nIntSize, int nFloatSize) {
    nSizeOfFloat = nFloatSize;
    nSizeOfInt = nIntSize;
    pInt = new int[nSizeOfInt];
    pFloat = new float[nSizeOfFloat];
    nNUmOfInt = 0;
    nNumOfFloat = 0;
}

void CArray::put(int n) {
    if (nNUmOfInt == nSizeOfInt) {
        std::cout << "full" << std::endl;
        return;
    }
    nNUmOfInt[pInt] = n;
    nNUmOfInt++;
}

void CArray::put(float x) {
    if (nNumOfFloat == nSizeOfFloat) {
        std::cout << "full" << std::endl;
        return;
    }

    nNumOfFloat[pFloat] = x;
    nNumOfFloat++;
}

int CArray::getInt(int index) {
    if (index >= nNUmOfInt) {
        return -1;
    }
    return index[pInt];
}

float CArray::getFloat(int index) {
    if (index >= nNumOfFloat) {
        return -1;
    }
    return index[pFloat];
}

CArray::~CArray() {
    delete[]pInt;
    delete[]pFloat;

}

void CArray::print() {
    for (int i = 0; i < nNUmOfInt; i++) {
        std::cout << i[pInt] << ",";
    }
    std::cout << std::endl;
    for (int i = 0; i < nNumOfFloat; i++) {
        std::cout << i[pFloat] << ",";
    }
    std::cout << std::endl;

}


int main() {
//    book q3
    Complex a(1, 2), b(2, 3.2);
    auto c3 = add(a, b);
    c3.print();

    //book q4
    s a4("hello world"), b4("wor"), c4("");
    std::cout << a4.is_empty() << c4.is_empty() << std::endl;
    std::cout << a4.compare(b4) << std::endl;
    std::cout << a4.find(b4) << std::endl;
    a4.empty();
    std::cout << a4.is_empty() << std::endl;
    a4.set("1080");
    std::cout << a4.a << std::endl;

    //book q5
    Point a5(2, 2), b5(1, 4);
    std::cout << a5.distance(b5) << std::endl;

    //ppt 3
    CPoint a3;
    a3.display();
    a3.setpoint(80, 150);
    a3.display();

    //ppt 4
    CTest c;
    c.init(200, 60);
    c.point();

    //ppt chapter 2 q1
    CSample ac2q1, bc2q1;
    ac2q1.init("abc", "edd open");
    ac2q1.print();
    bc2q1.init("bcd", "egg open");
    ac2q1.copy(bc2q1);
    ac2q1.print();
    ac2q1.free();

    //ppt chapter 2 q2
    CArray cArray;
    cArray.put(1);
    cArray.put(2);
    cArray.put(3);

    cArray.print();
    float x = 1.2, y = 1.3;
    cArray.put(x);
    cArray.put(y);
    cArray.print();
    std::cout << cArray.getInt(1) << cArray.getFloat(2) << std::endl;
    return 0;
}



